import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
// 引入path用于配置@别名
import path from "path";
// 引入svg需要用到的插件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
// mock插件导入
import { viteMockServe } from "vite-plugin-mock";

export default defineConfig(({ command, mode }) => {
  // 获取各种环境下对应的变量
  const env = loadEnv(mode, process.cwd());
  return {
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "@/styles/variable.scss";',
        },
      },
    },
    plugins: [
      vue(),
      // 配置svg
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",
      }),
      viteMockServe({
        // 保证开发阶段可以使用mock接口
        localEnabled: command === "serve",
      }),
    ],
    // 配置@别名
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    // 代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // 获取数据的服务器地址
          target: env.VITE_SERVE,
          // 需要代理跨域
          changeOrigin: true,
          // 路径重写
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
